package com.newtouch.servicehello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class ServiceHelloApplication {

    @Value("${hello.name}")
    private String helloName;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private int serverPort;
    public static void main(String[] args) {
        SpringApplication.run(ServiceHelloApplication.class, args);
    }

    @GetMapping("/")
    String sayHello() throws InterruptedException {
//        TimeUnit.MILLISECONDS.sleep(200);
        return String.format("Hello, current port is: %d", serverPort);
    }

    @GetMapping("/hello")
    String getHelloName() {
        return helloName;
    }

    @GetMapping("/user-instance")
    public List<ServiceInstance> showInfo() {
        return this.discoveryClient.getInstances("service-demo");
    }
}
